package 力扣._449_序列化和反序列化二叉搜索树;

import 力扣.公用数据结构.TreeNode;

public class Solution {
    void firstorder(TreeNode root, StringBuilder t) {
        if (root == null)
            return;
        t.append(root.val).append(',');
        firstorder(root.left, t);
        firstorder(root.right, t);
    }

    //BST,故只需要前序遍历即可
    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        StringBuilder s = new StringBuilder();
        firstorder(root, s);
        if (s.length() == 0)
            return "[]";
        else
            return "[" + s.substring(0, s.length() - 1) + "]";
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        data = data.substring(1, data.length() - 1);
        if (data.length() == 0)
            return null;
        if (!data.contains(",")) {
            return new TreeNode(Integer.parseInt(data));
        } else {
            String[] splits = data.split(",");
            //已知是BST，则根据前序即可建树
            return null;

        }
    }

    public static void main(String[] args) {
        TreeNode node = TreeNode.creat("[]");
        String serialize = new Solution().serialize(node);
        System.out.println(serialize);
    }
}
